package com.yonyougov.devops.cruxcisupport.rolebutton;

import com.yonyougov.devops.cruxcisupport.rolebutton.dto.AddMulRoleButtonDto;
import com.yonyougov.devops.cruxcisupport.rolebutton.vo.RoleButtonPageLikeDto;
import com.yonyougov.devops.cruxcisupport.rolebutton.vo.RoleButtonPageLikeVo;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * t_role_button服务实现
 */
@Service
public class RoleButtonServiceImpl extends BaseServiceImpl<RoleButtonEntity, BigInteger> implements RoleButtonService {
    private final RoleButtonDao roleButtonDao;

    public RoleButtonServiceImpl(RoleButtonDao roleButtonDao) {
        this.roleButtonDao = roleButtonDao;
    }

    @Override
    public BaseDao<RoleButtonEntity, BigInteger> getDao() {
        return roleButtonDao;
    }


    //<!--用户自定义代码开始-->
    @Override
    public Pagination<RoleButtonPageLikeVo> findRoleButtonPageLike(RoleButtonPageLikeDto roleButtonPageLikeDto) {
        List<RoleButtonPageLikeVo> roleButtonPageLike = roleButtonDao.findRoleButtonPageLike(roleButtonPageLikeDto);
        int totalCount = roleButtonDao.countByLikeRoleButton(roleButtonPageLikeDto);

        return new Pagination<RoleButtonPageLikeVo>(roleButtonPageLikeDto.getMax(), roleButtonPageLikeDto.getPageNum(), totalCount, roleButtonPageLike);
    }

    private void addBatch(List<RoleButtonEntity> params) {
        roleButtonDao.addBatch(params);
    }

    @Override
    public void deleteBatchRoleButtonByIds(List<BigInteger> ids) {
        roleButtonDao.deleteBatchRoleButtonByIds(ids);
    }

    @Override
    public void addMulRoleButton(AddMulRoleButtonDto addMulRoleButtonDto) {
        List<RoleButtonEntity> roleButtonEntities = new ArrayList<>();
        addMulRoleButtonDto.getBtnId().forEach(btnId -> {
            RoleButtonEntity roleButtonEntity = new RoleButtonEntity();
            roleButtonEntity.setId(new CruxDevOpsIdGenerator().generatorId(roleButtonDao));
            roleButtonEntity.setRoleId(addMulRoleButtonDto.getRoleId());
            roleButtonEntity.setBtnId(btnId);
            roleButtonEntities.add(roleButtonEntity);
        });
        addBatch(roleButtonEntities);
    }

    //<!--用户自定义代码结束-->
}
